package com.jketing.entity.ams.sys;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jketing.entity.SortEntity;

/**
 * Function 系统功能（菜单）实体
 * 
 * @author  李荣华
 * @version v1.0
 * @date     2017-08-16
 * @since   Jpa 2.1
 */
@JsonIgnoreProperties(value= {"parent","children"})
@Entity
@Table(name = "Function_")
public class Function extends SortEntity<Function>{
	private static final long serialVersionUID = -7653795800679336981L;
	/**
	 * parent:父级引用
	 */
	private Function parent;
	/**
	 * useable:启用状态
	 */
	private String useable;
	/**
	 * name:名称
	 */
	private String name;
	/**
	 * stamp:标记
	 */
	private String stamp;
	/**
	 * imageName:图片名称
	 */
	private String imageName;
	/**
	 * grade:树型级别
	 */
	private Integer grade;
	/**
	 * funType:功能类型
	 */
	private String funType;
	/**
	 * url:功能URL
	 */
	private String url;
	/**
	 * remark:备注
	 */
	private String remark; 
	/**
	 * children:下级集合
	 */
	private Set<Function> children = new HashSet<Function>();

	/**
	 * parent 由ParentId引用的上级系统菜单对象
	 */
	@ManyToOne
	@JoinColumn(name = "ParentId")
	public Function getParent() {
		return parent;
	}
	public void setParent(Function parent) {
		this.parent = parent;
	}
	
	/**
	 * useable 启用状态， true是启用，false是保留
	 */
	@Column(name="Useable_",nullable = false,length = 8)
	public String getUseable() {
		return useable;
	}
	public void setUseable(String useable) {
		this.useable = useable;
	}

	/**
	 * name 系统菜单名称
	 */
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * stamp 功能标记，用于权限控制
	 */
	public String getStamp() {
		return stamp;
	}
	public void setStamp(String stamp) {
		this.stamp = stamp;
	}
	
	/**
	 * imageName 菜单图片名称
	 */
	public String getImageName() {
		return imageName;
	}
	public void setImageName(String imageName) {
		this.imageName = imageName;
	}
	
	/**
	 *  grade 级别（在树形结构中的层次）
	 */
	@Column(name="grade_")
	public Integer getGrade() {
		return grade;
	}
	public void setGrade(Integer grade) {
		this.grade = grade;
	}

	/**
	 * funType 功能类型:
	 * 1是根;
	 * 2是菜单;
	 * 3是功能
	 */
	@Column(nullable = false,length=2)
	public String getFunType() {
		return funType;
	}
	public void setFunType(String funType) {
		this.funType = funType;
	}

	/**
	 * url 功能入口对应的页面 URL请求
	 * 功能类型是1时，url为null
	 */
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * 系统菜单的直接下级菜单，类型为2时，此集合个数为0
	 */
	@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@OrderBy(value = "id ASC")
	public Set<Function> getChildren() {
		return children;
	}
	public void setChildren(Set<Function> children) {
		this.children = children;
	}
	
	/**
	 * 增加下级
	 * @param entity
	 */
	public void add(Function entity) {
		if (!children.contains(entity)) {
			children.add(entity);
			entity.setParent(this);
		}
	}	

	/**
	 * 删除下级
	 * @param entity
	 */
	public void remove(Function entity) {
		if (children.contains(entity)) {
			entity.setParent(null);
			children.remove(entity);
		}
	}
	
	/**
	 * remark 备注
	 */
	public String getRemark() {
		return remark;
	}
	public void setRemark(String remark) {
		this.remark = remark;
	}

	/**
	 * 	equals 判断实例是否相等
	 * 
	 * 	是本类实例并且实例id值相等返回true
	 * 	其它情况返回false
	 * 
	 * 	@param object 其它实例
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof Function)) {
			return false;
		}
		Function other = (Function) object;
		if(!id.equals(other.id)){
			return false;
		}
		return true;
	}
	
	/**
	 * compareTo Comparable接口方法
	 * 	实体对象比较
	 * 	根据排序号差比较：本实例排序号减去其它同类实例排序号
	 * 	差大于0返回1
	 * 	差等于0返回0
	 * 	差小于0返回-1
	 * 	参数other为null或者不是本类的实例返回-1
	 * 
	 * @param  other 其它同类实例
	 */
	public int compareTo(Function other) {
		if (other == null || !(other instanceof Function))
			return -1;
		long temp = orderNumber - other.getOrderNumber();
		if (temp > 0)
			return 1;
		else if (temp == 0)
			return 0;
		else
			return -1;
	}
}
